package mspbots.next.ticket.core.data.rules;

import lombok.RequiredArgsConstructor;
import mspbots.next.ticket.core.AbstractToolsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;

/**
 * mspbots.next.ticket.core.data.rules.RuleTemplateServiceImpl
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/12/15
 */
@Service
@RequiredArgsConstructor
public class RulesTemplatesServiceImpl extends AbstractToolsUtil {

    private final RulesTemplatesRepository repository;

    public Flux<RulesTemplate> findByAll() {
        return this.repository.findAll();
    }

    public Mono<RulesTemplate> saveOne(RulesTemplateRequest request) {
        RulesTemplate rule = new RulesTemplate();
        BeanUtils.copyProperties(request, rule);
        return this.repository.save(rule);

    }

    public Flux<RulesTemplate> batchSave(List<RulesTemplateRequest> requests) {

        List<RulesTemplate> rules = requests.parallelStream().map(request -> {
            RulesTemplate rule = new RulesTemplate();
            BeanUtils.copyProperties(request, rule);
            return rule;
        }).collect(Collectors.toList());

        return this.repository.saveAll(rules);

    }
}
